package com.acme.logginglab.appenders;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.Properties;

import com.acme.logginglab.mylogger.LogLevel;

public class ConsoleAppender implements Appender {
	private final String propertyFileName = "/mylogger.properties";
	private static ConsoleAppender instance = null;
	private String format;
	
	public static ConsoleAppender getInstance() {
		if (instance == null) {
			instance = new ConsoleAppender();
		}
		return instance;
	}
	
	public ConsoleAppender() {
		InputStream in = getClass().getResourceAsStream(propertyFileName);
		Properties props = new Properties();
		try {
			props.load(in);
			format = props.getProperty("mylogger.appender.format");
			if (format == null) {
				format = "DEFAULT %d %l %m [%t]";
			}
		} catch (IOException e) {
			format = "DEFAULT %d %l %m [%t]";
		}
	}

	public void append(LogLevel l, String msg) {
		String formattedMsg = format.replace("%m", msg)
				.replace("%n", "\n")
				.replace("%d", new Date().toString())
				.replace("%l", l.toString())
				.replace("%t", Thread.currentThread().getName())
				;

		System.out.print(formattedMsg);
	}

}
